<?php
/***
	客户和潜在客户
**/
class Client extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->model('client_model');
		$this->load->model('contact_person_model');
		$this->load->helper('url');
	}
	//显示所有客户和最近两次更新的联系记录
	public function index($type = 0) {
		$data ['clients'] = $this->client_model->get_all();
				
		$data['title'] = '所有潜在客户表';
		
		//加载模版解析类
		$this->load->library('parser');
		
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		//type == 0 加载不含范围的表格
		if ($type == 0) {
			$this->parser->parse('clients/list', $data);
		}
		//type == 1 加载含范围的表格
		if ($type == 1) {
			$this->parser->parse('clients/list_scope', $data);
		}
		$this->load->view('templates/foot');
	}
		
	
	//新增客户信息
	public function create() {
		//如果POST传来数据，去insert并跳转新建联系人，需要传入Client_id, client_name, title；
		//否则打开表单
		
		if (isset($_POST['name'])) {
			
			//返回client_id和client_name
			$data = $this->client_model->create();
			
			$data ['contact_persons'] = $this->contact_person_model->get_by_client($data['client_id']);
			$data['title'] = '联系人表';
			
			//加载模版解析类
			$this->load->library('parser');
			
			$this->load->view('templates/head');
			$this->load->view('templates/menu');
			$this->parser->parse('contact_person/form_create', $data);
			$this->parser->parse('contact_person/list', $data);
			$this->load->view('templates/foot');
		}
		else {
			//获取所有客户名
			$data ['clients'] = $this->client_model->get_name_all();
			
			//加载模版解析类
			$this->load->library('parser');
			
			$this->load->view('templates/head');
			$this->load->view('templates/menu');
			$this->parser->parse('clients/form_create', $data);
			$this->load->view('templates/foot');
		}
		
	}
	
	//编辑客户信息
	public function edit($id = 0) {
		if (isset($_POST['id'])) {
			//去update这个id
			$id = $this->input->post('id');
			$this->client_model->update_all($id);
			
			
			$this->load->view('templates/head');
			$this->load->view('templates/menu');
			echo "<br />";
			echo "<p class='text-center lead'>客户信息修改成功！</p>";
			$this->load->view('templates/foot');
		}
		else {
			//根据id查寻客户信息
			$data  = $this->client_model->get_by_id($id);
						
			//加载模版解析类
			$this->load->library('parser');
			
			$this->load->view('templates/head');
			$this->load->view('templates/menu');
			$this->parser->parse('clients/form_edit', $data);
			$this->load->view('templates/foot');
		}
	}
	
	//校核客户信息
	public function check($id = 0) {
		//如果Get一个id那么修改check为YES，并显示校核成功信息；
		//列出未校核表
		if ($id <> 0) {
			//去update这个id
			$this->client_model->update_check ($id);
		}
		//获取所有未校核客户信息
		$data ['clients'] = $this->client_model->get_uncheck();
		
		
		$data['title'] = '未校核客户信息表';
		
		//加载模版解析类
		$this->load->library('parser');
		
		$this->load->view('templates/head');
		$this->load->view('templates/menu');
		$this->parser->parse('clients/list_uncheck', $data);
		$this->load->view('templates/foot');
	}
	
	//以下 Easyui 专用
	public function easyui_index( ) {
	
		$this->load->view('templates/easyui_head');
		$this->load->view('clients/easyui_list');
		$this->load->view('templates/easyui_foot');
	
	}
	
	public function easyui_get( ) {
		$result['rows'] = $this->client_model->get_all();
		echo json_encode($result);
		
	}
	
	//test
	
}